package statistics

import (
	"admin100/app/model"
	"admin100/app/model/gamemodel"
	"admin100/app/service"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"
	"admin100/library/utils"
	"strings"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

var RemainMgr = remainMgrApi{}

type remainMgrApi struct{}

func (a *remainMgrApi) Total(r *ghttp.Request) {
	var req model.TotalRemainReq

	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	if req.EndTime < req.StartTime || req.StartTime == 0 {
		defErr.Panic(define.TimeErrStartEnd)
	}

	data, total := service.Remain.FetchRemainTotalList(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

func (a *remainMgrApi) Charge(r *ghttp.Request) {
	var req model.ChargeRemainReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	if req.EndTime < req.StartTime || req.StartTime == 0 {
		defErr.Panic(define.TimeErrStartEnd)
	}

	data := service.Remain.FetchRemainChargeList(&req)
	response.JsonSuc(r, g.Map{"total": 100, "rows": data})
}

func (a *remainMgrApi) Active(r *ghttp.Request) {
	var req model.ActiveRemainReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	if req.EndTime < req.StartTime || req.StartTime == 0 {
		defErr.Panic(define.TimeErrStartEnd)
	}

	total, data := service.Remain.FetchActiveList(&req)
	response.JsonSuc(r, g.Map{"total": total, "rows": data})
}

func (a *remainMgrApi) Level(r *ghttp.Request) {
	var req = gamemodel.RemainCommonReq{}
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	if req.EndTime < req.StartTime {
		defErr.Panic(define.TimeErrStartEnd)
	}
	channelSlice := utils.TrimSplitStr(req.ChannelList)
	data := service.Remain.FetchRemainLevel(req.PlatformId, req.ServerId, channelSlice, req.StartTime, req.EndTime, req.IsChargePlayer)
	response.JsonSuc(r, g.Map{"rows": data})
}

func (a *remainMgrApi) Task(r *ghttp.Request) {
	var req = gamemodel.RemainCommonReq{}
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	channelSlice := utils.TrimSplitStr(req.ChannelList)
	response.JsonSuc(r, g.Map{"rows": service.Remain.FetchTaskRemain(req.PlatformId, req.ServerId, channelSlice, req.IsChargePlayer)})
}

func (a *remainMgrApi) Time(r *ghttp.Request) {
	var req = gamemodel.RemainCommonReq{}
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	if req.EndTime < req.StartTime {
		defErr.Panic(define.TimeErrStartEnd)
	}
	channelSlice := utils.TrimSplitStr(req.ChannelList)
	data := service.Remain.FetchRemainTime(req.PlatformId, req.ServerId, channelSlice, req.StartTime, req.EndTime, req.IsChargePlayer)
	response.JsonSuc(r, g.Map{"rows": data})
}

func (a *remainMgrApi) RealTimeOnline(r *ghttp.Request) {
	var req = model.RealTimeOnlineReq{}
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	channelSlice := strings.Split(req.ChannelList, ",")
	data := service.Statistics.GetServerOnlineStatistics(req.PlatformId, req.ServerId, channelSlice)
	response.JsonSuc(r, g.Map{"rows": data})
}
